//-----------------------------------------------------------------------------
#include "EXTI.h"

//-----------------------------------------------------------------------------
void EXTI_Configuration(void)
{
  EXTI_InitTypeDef EXTI_InitStruct = {0};
  CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
  CKCUClock.Bit.EXTI    = 1;
  CKCUClock.Bit.AFIO    = 1;
  CKCU_PeripClockConfig(CKCUClock, ENABLE);

  AFIO_EXTISourceConfig(AFIO_EXTI_CH_0,AFIO_ESS_PA);
  EXTI_InitStruct.EXTI_Debounce = EXTI_DEBOUNCE_DISABLE;
  EXTI_InitStruct.EXTI_DebounceCnt = 0;
  EXTI_InitStruct.EXTI_Channel = AFIO_EXTI_CH_0;
  EXTI_InitStruct.EXTI_IntType = EXTI_POSITIVE_EDGE;
  EXTI_Init(&EXTI_InitStruct);
  EXTI_IntConfig(AFIO_EXTI_CH_0, ENABLE);

  AFIO_EXTISourceConfig(AFIO_EXTI_CH_1,AFIO_ESS_PA);
  EXTI_InitStruct.EXTI_Debounce = EXTI_DEBOUNCE_DISABLE;
  EXTI_InitStruct.EXTI_DebounceCnt = 0;
  EXTI_InitStruct.EXTI_Channel = AFIO_EXTI_CH_1;
  EXTI_InitStruct.EXTI_IntType = EXTI_POSITIVE_EDGE;
  EXTI_Init(&EXTI_InitStruct);
  EXTI_IntConfig(AFIO_EXTI_CH_1, ENABLE);

  AFIO_EXTISourceConfig(AFIO_EXTI_CH_2,AFIO_ESS_PA);
  EXTI_InitStruct.EXTI_Debounce = EXTI_DEBOUNCE_DISABLE;
  EXTI_InitStruct.EXTI_DebounceCnt = 0;
  EXTI_InitStruct.EXTI_Channel = AFIO_EXTI_CH_2;
  EXTI_InitStruct.EXTI_IntType = EXTI_POSITIVE_EDGE;
  EXTI_Init(&EXTI_InitStruct);
  EXTI_IntConfig(AFIO_EXTI_CH_2, ENABLE);

  AFIO_EXTISourceConfig(AFIO_EXTI_CH_3,AFIO_ESS_PA);
  EXTI_InitStruct.EXTI_Debounce = EXTI_DEBOUNCE_DISABLE;
  EXTI_InitStruct.EXTI_DebounceCnt = 0;
  EXTI_InitStruct.EXTI_Channel = AFIO_EXTI_CH_3;
  EXTI_InitStruct.EXTI_IntType = EXTI_POSITIVE_EDGE;
  EXTI_Init(&EXTI_InitStruct);
  EXTI_IntConfig(AFIO_EXTI_CH_3, ENABLE);

  AFIO_EXTISourceConfig(AFIO_EXTI_CH_4,AFIO_ESS_PA);
  EXTI_InitStruct.EXTI_Debounce = EXTI_DEBOUNCE_DISABLE;
  EXTI_InitStruct.EXTI_DebounceCnt = 0;
  EXTI_InitStruct.EXTI_Channel = AFIO_EXTI_CH_4;
  EXTI_InitStruct.EXTI_IntType = EXTI_POSITIVE_EDGE;
  EXTI_Init(&EXTI_InitStruct);
  EXTI_IntConfig(AFIO_EXTI_CH_4, ENABLE);


  NVIC_EnableIRQ(EXTI0_1_IRQn);
  NVIC_EnableIRQ(EXTI2_3_IRQn);
  NVIC_EnableIRQ(EXTI4_15_IRQn);
}

//-----------------------------------------------------------------------------
void EXTI0_1_IRQHandler(void)
{
  if (EXTI_GetEdgeStatus(EXTI_CHANNEL_0, EXTI_EDGE_POSITIVE))
  {
    EXTI_ClearEdgeFlag(EXTI_CHANNEL_0);
    //add code here
  }

  if (EXTI_GetEdgeStatus(EXTI_CHANNEL_1, EXTI_EDGE_POSITIVE))
  {
    EXTI_ClearEdgeFlag(EXTI_CHANNEL_1);
    //add code here
  }

  //The signs maybe have been updated in the IRQ processing function
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_0);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_1);
}

//-----------------------------------------------------------------------------
void EXTI2_3_IRQHandler(void)
{
  if (EXTI_GetEdgeStatus(EXTI_CHANNEL_2, EXTI_EDGE_POSITIVE))
  {
    EXTI_ClearEdgeFlag(EXTI_CHANNEL_2);
    //add code here
  }

  if (EXTI_GetEdgeStatus(EXTI_CHANNEL_3, EXTI_EDGE_POSITIVE))
  {
    EXTI_ClearEdgeFlag(EXTI_CHANNEL_3);
    //add code here
  }

  //The signs maybe have been updated in the IRQ processing function
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_2);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_3);
}

//-----------------------------------------------------------------------------
void EXTI4_15_IRQHandler(void)
{
  if (EXTI_GetEdgeStatus(EXTI_CHANNEL_4, EXTI_EDGE_POSITIVE))
  {
    EXTI_ClearEdgeFlag(EXTI_CHANNEL_4);
    //add code here
  }

  //The signs maybe have been updated in the IRQ processing function
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_4);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_5);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_6);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_7);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_8);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_9);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_10);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_11);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_12);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_13);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_14);
  //EXTI_ClearEdgeFlag(EXTI_CHANNEL_15);
}

//-----------------------------------------------------------------------------


